import dbm,dbm.gnu

# 写
with dbm.gnu.open('test.dbm', 'c') as db:
    # Record some values
    db[b'hello'] = b'there'
    db['www.python.org'] = 'Python Website'
    db['www.cnn.com'] = 'Cable News Network'

    'dbm只支持字符串和字节，不支持其他类型'
    # db['www.yahoo.com'] = 4     # --NG

# 读
with dbm.gnu.open('test.dbm', 'r') as db:
    assert db['www.cnn.com'] == b'Cable News Network'
    print(db[b'www.python.org'])
    print(db.get('python.org', b'not present'))
"""
b'Python Website'
b'not present'

"""

# 遍历形式
with dbm.gnu.open('test.dbm', 'r') as db:
    print("key 遍历形式")
    k = db.firstkey()
    while k != None:
        print(k, db[k])
        k = db.nextkey(k)
